package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.ExamPaperQuestionMapper;
import com.zhijian.medical.entity.po.ExamPaperQuestion;
import com.zhijian.medical.entity.vo.request.ExamPaperQuestionSearchVo;
import com.zhijian.medical.entity.vo.response.ExamPaperQuestionVo;
import com.zhijian.medical.service.ExamPaperQuestionService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 考核-试卷生成的题目表业务处理层
 * 
 * @author HCG
 * @version: 1.0
 * @date 2022-01-18 18:27:06
 */
@Slf4j
@Service
public class ExamPaperQuestionServiceImpl implements ExamPaperQuestionService {

	@Autowired
    private ExamPaperQuestionMapper mapper;

    @Override
    public ExamPaperQuestion selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(ExamPaperQuestion examPaperQuestion) {
        Date now = new Date();
        examPaperQuestion.setCreateTime(now);
        examPaperQuestion.setUpdateTime(now);
        return mapper.insert(examPaperQuestion) > 0 ? examPaperQuestion.getId() : 0L;
    }

    @Override
    public boolean update(ExamPaperQuestion examPaperQuestion) {
        Date now = new Date();
        examPaperQuestion.setUpdateTime(now);
        return mapper.updateByPrimaryKey(examPaperQuestion) != 0;
    }

    @Override
    public Integer count(ExamPaperQuestionSearchVo examPaperQuestionSearchVo) {
        return mapper.count(examPaperQuestionSearchVo);
    }

    @Override
    public List<ExamPaperQuestionVo> selectList(ExamPaperQuestionSearchVo examPaperQuestionSearchVo) {
        return mapper.selectList(examPaperQuestionSearchVo);
    }

    @Override
    public Page<ExamPaperQuestionVo> selectPage(ExamPaperQuestionSearchVo examPaperQuestionSearchVo) {
        //先直接查总数
        int count = count(examPaperQuestionSearchVo);
        //分页
        Page<ExamPaperQuestionVo> page = new Page<>(true, count, examPaperQuestionSearchVo.getPageNum(), examPaperQuestionSearchVo.getPageSize());
        examPaperQuestionSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ExamPaperQuestionVo> responseVos = this.selectList(examPaperQuestionSearchVo);
        page.setData(responseVos);
        return page;
    }
    
}